/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblProjectDetail;
import com.aptech.entities.TblWorking;
import com.aptech.mBean.util.PaginationHelper;
import com.aptech.sBean.TblElevationFacade;
import com.aptech.sBean.TblProjectDetailFacade;
import com.aptech.sBean.TblProjectFacade;
import com.aptech.sBean.TblWorkingFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author LanhAnh
 */
@ManagedBean(name = "projectDetailController")
@SessionScoped
public class ProjectDetailController implements Serializable{
    @EJB
    private TblProjectFacade tblProjectFacade;
    

    @EJB
    private TblProjectDetailFacade tblProjectDetailFacade;
    private DataModel items = null;
    private PaginationHelper paginationHelper;
    private TblProjectDetail current;
    private int selectedItemIndex;

    public TblProjectDetailFacade getFacade() {
        return tblProjectDetailFacade;
    }

    public TblProjectDetail getSelected() {
        if (current == null) {
            current = new TblProjectDetail();
            current.setTblProjectDetailPK(new com.aptech.entities.TblProjectDetailPK());
            selectedItemIndex = -1;
        }
        return current;
    }

    public PaginationHelper getPaginationHelper() {
        if (paginationHelper == null) {
            paginationHelper = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return paginationHelper;
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPaginationHelper().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        paginationHelper = null;
    }

//    public String prepareView(TblProjectDetail b) {
//        current = b;
//        return "ProjectDetailManagement";
//    }
    public String prepareList() {
        recreateModel();
        return "ProjectManagement";
    }

    public void onEdit(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edited Project", ((TblProjectDetail) e.getObject()).getTblProjectDetailPK().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        getFacade().edit((TblProjectDetail) e.getObject());
    }

    public void onEditCancel(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edit ProjectDetail Cancelled", ((TblProjectDetail) e.getObject()).getTblProjectDetailPK().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String prepareCreate() {
        current = new TblProjectDetail();
        current.setTblProjectDetailPK(new com.aptech.entities.TblProjectDetailPK());
        selectedItemIndex = -1;
        return "ProjectManagement";
    }
 
    public String create() {
        try {
            getFacade().create(current);
            return prepareCreate();
        } catch (Exception e) {
            return null;
        }
    }

    @FacesConverter(forClass = TblProjectDetail.class)
    public static class BookingDetailsControllerConverter implements Converter {

        private static final String SEPARATOR = "#";
        private static final String SEPARATOR_ESCAPED = "\\#";

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ProjectDetailController controller = (ProjectDetailController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "projectDetailsController");
            return controller.tblProjectDetailFacade.find(getKey(value));
        }
            
        com.aptech.entities.TblProjectDetailPK getKey(String value) {
            com.aptech.entities.TblProjectDetailPK key;
            String values[] = value.split(SEPARATOR_ESCAPED);
            key = new com.aptech.entities.TblProjectDetailPK();
            key.setPrID(Integer.parseInt(values[0]));
            key.setPrID(Integer.parseInt(values[1]));
            return key;
        }

        String getStringKey(com.aptech.entities.TblProjectDetailPK value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value.getPrID());
            sb.append(SEPARATOR);
            sb.append(value.getElID());
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TblProjectDetail) {
                TblProjectDetail o = (TblProjectDetail) object;
                return getStringKey(o.getTblProjectDetailPK());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TblProjectDetail.class.getName());
            }
        }
    }

    /**
     * Creates a new instance of ProjectDetailController
     */
  
    public ProjectDetailController() {
    }
    
}
